
import { useInitTable } from '~/composables/useCommon.js'
import { ref } from 'vue';
import { sellibrary } from '~/api/zoology'
import { toast, showModal } from '~/composables/util'
import { getStorage } from '~/composables/storage'
import { router } from "~/router"


export const options = ref([
    {
        label: "全部文档类型",
        value: "-1"
    },
    {
        label: "免费",
        value: "0"
    },
    {
        label: "付费",
        value: "1"
    }
])

//   export const activeIndex = ref(0)
export const list = ref(['投智官方', '众创文库'])
export const tapItems = (e) => {
    activeIndex.value = e
    searchForm.type = (e+1)
    currentPage.value = 1
    getData()
}


export const activeIndex = ref(0)


export const {
    currentPage,
    total,
    limit,
    getData,
    tableData,
    loading,
    searchForm,
} = useInitTable({
    getList: sellibrary,
    isUser: true,
    searchForm: {
        ispay: '-1',
        type: 1
    },
    limit: 5
})

export const onUpdate = (e, option) => {
    searchForm.ispay = e
    currentPage.value = 1
    getData()
}

export const tapGoPay = (item) => {
    let userType = getStorage('userType')
    let userMessage = getStorage('userMessage')




    if (item.price == 0 || item.isbuy == 1) {
        return window.open(item.attachment)
    }





    if (!userType) {
        toast('请先登录','warning')
        return router.push('/login')
    }

    let userId = ''
    if (userType == 1) {
        userId = userMessage.orgId
    } else {
        userId = userMessage.userId
    }


    showModal("是否要购买？").then(res => {

        let data = {
            buyerid: userId,
            buyertype: userType,
            classid: item.libraryId, //商品id
            classname: item.fileName, //商品名称
            price: item.price,
            // classaddress:detailValue.value.image  //图片地址
            placetype:2
        }
        router.push({
            name: 'shopPay',
            query: {
                arguments: encodeURIComponent(JSON.stringify(data))
            }
        })
    })




}